<%@ page import="au.com.identityconcepts.windowslive.sso.*, au.com.identityconcepts.windowslive.sso.ws.*" %>

<jsp:useBean id="Session" class="au.com.identityconcepts.windowslive.sso.SSOBean" scope="session">
<%
      SSOBean initsb = (SSOBean) session.getAttribute("SSOBean");
      if (initsb == null) {
          initsb = new SSOBean();
          session.setAttribute("SSOBean",initsb);        
      }         
%>
</jsp:useBean>

<%
      String username = null;
      String service = null;
      String slt = null;
      String url = null;
      boolean redirect = false;
  	  boolean debug = true;

	  SSOBean sb = (SSOBean) session.getAttribute("SSOBean");
	  
	  // Obtain the current session
	  if (sb != null) {		  
		  	  
		  if (request.getParameter("username") != null) {
			  service = request.getParameter("service");
			  username = request.getParameter("username");
	          redirect = Boolean.parseBoolean(request.getParameter("redirect"));
	          debug = Boolean.parseBoolean(request.getParameter("debug"));
		  } else {
			  service = request.getHeader("service");                 
		      username = request.getHeader("username");
		      redirect = Boolean.parseBoolean(request.getHeader("redirect"));
		      debug = Boolean.parseBoolean(request.getHeader("debug"));  
		  }
		  
		  
		  if (debug) {
	%>    
	          Service <%=service%>
	          Username <%=username%>
	          Redirect <%=redirect%>
	          Debug <%=debug%>
	          loginURL Live = <%=sb.session.loginURLLive%> 
	          loginURL Exchange Labs = <%=sb.session.loginURLExchangeLabs%>
	          loginURL ITA = <%=sb.session.loginURLITA%>
	<%        
		  }
		  
		  // Only proceed if a username has been passed
		  if (username != null)
			    slt = sb.getSLT(username).trim().toLowerCase();
		  else
			    return;		  
		  
		  // Cleanup the variables
		  if (service != null)
			  service = service.trim().toLowerCase();	  
		  else
			  service = "slt";
		  
		  // Get the SLT and build the URL based on the service, or just return the SLT
		  // TODO Build a hashmap of all services (when known!)
		  slt = Marshall.GetSLT(sb.session,username, 100);      	      		  
	      if (service.equals("livemail")) {
	    	  url = sb.session.loginURLLive + "&ct=" + (System.currentTimeMillis()*1000) + "&slt=" + slt;	 
	          if (debug) {
	        	  %>    
	        	       livemail selected <%=url%>
	        	  <%
	          }
	      }
	      else if (service.equals("exchangelabs")) {
	          url = sb.session.loginURLExchangeLabs + "&id=" + sb.session.siteID + "&ct=" + (System.currentTimeMillis()*1000) + "&slt=" + slt;
	          if (debug) {
	              %>    
	                   exchangelabs selected <%=url%>
	              <%
	          }
	      }
	      else {
	    	  url = slt;      
	          if (debug) {
	              %>    
	                   no service selected <%=url%>
	              <%        
	          }    	  
	      }
	      
	      // Return URL or SLT
	      if (url != null) {      
		      // Determine if a redirect has been requested	      
		      if ( (redirect) && ((service != null) || (service.equalsIgnoreCase("slt"))) ) {
		          response.setStatus(301);    
		          response.setHeader("Connection", "close");
		          response.sendRedirect(url);             
		      }      
		      else {
	%>            
	              <%=url%>
	<%            
		      }
	      }
	      else {
	%>            
	Error processing request. SLT is null
	Service <%=service%>
	Username <%=username%>
	Redirect <%=redirect%>
	Debug <%=debug%>
	loginURL Live = <%=sb.session.loginURLLive%> 
	loginURL Exchange Labs = <%=sb.session.loginURLExchangeLabs%>
	<%        
	   	  }
	}
%>    
 

